package cn.xiqi.cms.hibernate.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.hibernate.DAO.UsrDAO;
import cn.xiqi.cms.hibernate.beans.Usrinfo;

public class UsrServiceImpl implements UsrService {
	private UsrDAO dao;

	@Override
	public int insert(Usrinfo record) {
		int id = 0;
		
		id = dao.insert(record);
		return id;
	}

	@Override
	public boolean delete(Usrinfo record) {
		boolean b = dao.delete(record);
		return b;
	}

	@Override
	public boolean update(Usrinfo record) {
		boolean b = dao.update(record);
		return b;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Usrinfo getByID(int id) {
		Usrinfo record = null;
		Map values = new HashMap<String, Object>();
		
		values.put("type", "id");
		values.put("id", id);
		record = dao.select(values).get(0);
		return record;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Usrinfo> getByDepart(String depart, int page, int size) {
		List<Usrinfo> record = null;
		Map values = new HashMap<>();
		
		values.put("type", "depart");
		values.put("depart", depart);
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}
	
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Usrinfo> getByName(String name, int page, int size) {
		List<Usrinfo> record = null;
		Map values = new HashMap<>();
		
		values.put("type", "name");
		values.put("name", name);
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}

	@Override
	public void setDao(UsrDAO dao) {
		this.dao = dao;
	}

	@Override
	public List<Usrinfo> getAll(int page, int size) {
		List<Usrinfo> record = null;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "all");
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}

	@Override
	public int getCount(String type) {
		int count = 0;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", type);
		count = dao.getCount(values);
		return count;
	}

	@Override
	public List<Usrinfo> getByRole(int roleID, int page, int size) {
		List<Usrinfo> record = null;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "role");
		values.put("role", roleID);
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}

	@Override
	public List<Usrinfo> getByStat(int stat, int page, int size) {
		List<Usrinfo> record = null;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "stat");
		values.put("stat", stat);
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}

	@Override
	public List<Usrinfo> getByLevel(int level, int page, int size) {
		List<Usrinfo> record = null;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "level");
		values.put("level", level);
		if(size!=0){
			values.put("page", page);
			values.put("size", size);
		}
		record = dao.select(values);
		return record;
	}

	@Override
	public boolean setRole(int userID, int roleID) {
		Usrinfo record = getByID(userID);
		boolean b = false;
		
		record.setRoleID(roleID);
		b = dao.update(record);
		return b;
	}

	@Override
	public boolean setStat(int userID, int stat) {
		Usrinfo record = getByID(userID);
		boolean b = false;
		
		record.setStat(stat);
		b = dao.update(record);
		return b;
	}


	@Override
	public boolean setLevel(int userID, int level) {
		Usrinfo record = getByID(userID);
		boolean b = false;
		
		record.setLevel(level);
		b = dao.update(record);
		return b;
	}
}
